Method and system for global constant management for memory

ABSTRACT

Method and system for global constant management. A method of operating a computer is described in which, for data structures and a set of data structures, the date structure is received from a first memory. The data structure includes one or more sets of instructions and a set of one or more constants. The data structure is stored in a second memory. If constants from the data structure have not been stored in other data structures in the second memory, other than the first data structure, then constants in the data structures are stored in data structures in second memory. The constants from the first data structure in the second memory are replaced with links to respective other data structures in the second memory. In one example system, the data structure from the first memory comprises a Java class, and the sets of instructions comprise Java methods. The constants from the data structure in the first memory may comprise a constant pool.

CROSS REFERENCE TO RELATED APPLICATION

[0001] This application is related to application Ser. No. ______, filedon the same day as the present application, entitled, Method and Systemfor Dynamically Loading Data Structures into Memory With Global ConstantPool, [Attorney Docket No. 17201.707], which is hereby incorporatedherein by reference in its entirety.

BACKGROUND

[0002] 1. Field of the Invention

[0003] The invention relates to managing constants in memory, inparticular for constants associated with data structures into memory.

[0004] 2. Description of Related Art

[0005] Java is an object oriented programming language, which is oftenused in a network environment, for example, the Internet. Java's sourcecode is written, and then the source code is compiled into a series ofclass files. The class files can be stored remotely, for example on aserver and then be loaded dynamically when needed on a local system. Theclass files include bytecode, a set of instructions lower level than theoriginal Java source code, yet higher level than code specific to aparticular processor. This helps to allow Java to be particularly suitedfor the network environment, so that a variety of different localsystems can run the Java programs from a network server. Java classescan be distributed to a variety of different systems, as may beconnected to the Internet. For example, when encountering a Web page viaa browser, a Java application may be initiated, which would involve theJava class files being loaded via the Internet on to the local system.

[0006] A local system that runs the Java classes needs functionality tointerpret the Java bytecode. One system that provides such functionalityis a Java Virtual Machine. The Java Virtual Machine loads the respectiveclasses from the class files and executes methods as needed. The JavaVirtual Machine is typically implemented in software, often associatedwith a browser, but may also be implemented in hardware.

[0007] In order to provide useful network applications to a wide varietyof systems, it is desirable to be able to run Java applications on smallsystems that may not have a large amount of memory. Because such systemsare small and may not possess excessive memory, it is helpful toconserve the use of memory used by the Java application, in particularthe use of random access memory (read-write memory). One approach is topreload classes into memory, loading into read-only memory the methodsand data that do not vary, while loading into random access memoryvarying data and methods. Such an approach is described in U.S. Pat. No.5,815,718, entitled “Method And System For Loading Classes In Read-OnlyMemory,” invented by T. Tock, (hereinafter, “Tock”), which isincorporated herein by reference in its entirety. Classes in Javainclude typically a number of constants. These constants may require asignificant amount of memory on the local system that is running theJava program. The Tock patent indicates that the offline class loadereliminates duplicate constants, in order to combine the constant poolsof all the classes in a space efficient manner.

[0008] It would be desirable to provide a method and a system whichovercome the deficiencies of the prior art.

SUMMARY OF THE INVENTION

[0009] The invention includes a method and system for global constantmanagement. A method of operating a computer is described in which, fordata structures and a set of data structures, the date structure isreceived from a first memory. The data structure includes one or moresets of instructions and a set of one or more constants. The datastructure is stored in a second memory. If constants from the datastructure have not been stored in other data structures in the secondmemory, other than the first data structure, then constants in the datastructures are stored in data structures in second memory. The constantsfrom the first data structure in the second memory are replaced withlinks to respective other data structures in the second memory.

[0010] In one example system, the data structure from the first memorycomprises a Java class, and the sets of instructions comprise Javamethods. The constants from the data structure in the first memory maycomprise a constant pool. Receiving the data structure from a firstmemory may comprise receiving the data structure from a server over theInternet.

[0011] One embodiment of the invention comprises a computer system thatincludes a processor and a memory. The memory includes a set of objects,each object including a constant, and a set of classes. The classescorrespond to classes from a class file, in which classes have sets ofmethods and sets of constants. The memory includes a first class in theset of classes that has a link to an object in a set of objects. Theobject includes a constant from a class in a class file that correspondsto the first class. The first class in the memory does not include theconstant. The memory includes a second class in the set of classes thathas a link to the object in the set of objects. A class in the classfile that corresponds to the second class has the constant. The secondclass in the memory does not include the constant.

[0012] According to one embodiment of the invention, the class file thatcorresponds to the first class in the set of classes includes a constantpool. The constant pool includes strings and constants other thanstrings. The first class in the memory includes constants other thanstrings and links to objects in the set of objects that include stringscorresponding to strings in the constant pool in the class file.

[0013] Embodiments of the invention include computer readable code thatimplements the methods described herein. According to one embodiment ofthe invention, the computer readable code is embodied in a computerreadable medium, and according to another embodiment in the invention,the computer readable code is embodied in a carrier wave that may betransmitted from one computer system to another, for example, over anetwork.

DESCRIPTION OF THE DRAWINGS

[0014] The invention is illustrated by way of example, and notlimitation in the drawings.

[0015]FIG. 1 is block diagram of a virtual machine, memory, and system,according to an embodiment of the invention.

[0016]FIG. 2 is a flow chart of a method of loading classes into memory,according to an embodiment of the invention.

[0017]FIG. 3 is a flow chart of a method of storing classes in ROM,according to an embodiment of the invention.

DETAILED DESCRIPTION

[0018] The following is a description of embodiments of the invention.The embodiments shown help to illustrate the invention. However, it isnot intended that the invention be limited to the precise embodimentsshown.

[0019] Java classes are stored in Java class files. Each class typicallyincludes a constant pool, which contains a set of constants used by theclass. Constants from one class are often duplicated in other classes.This duplication can result in a waste of memory if the constants,particularly, large constants, are stored redundantly in the system'smemory. The Java Virtual Machine loads classes to be executed from theclass files. According to one embodiment of the invention, at least someof the constants from the constant pool of a class are stored in objectsseparate from the class. The corresponding constants in the constantpool are replaced with links to the objects that contain the respectiveconstants. For other classes, constants are replaced with links toobjects containing the respective constants. Objects containing theconstants are shared between multiple classes that link to them. Forexample the constant pools of both class A and class B may have a string“ClassNameC.” An object containing “ClassNameC” is created, and In theconstant pools of both class A and B, the constant “ClassNameC” isreplaced with a link to the object containing “ClassNameC.” According toone embodiment of the invention, all strings from constant pools of thevarious classes are stored in objects separate from the classes, butonly commonly used strings are shared among different classes.

[0020] Thus, an advantage of such an approach is that memory isconserved that would otherwise be wasted by constant pool entriesredundant across various classes. Additionally, since the constants arestored in objects linked to by the classes, the objects containing theconstants can be garbage collected when they are no longer needed. Thisis possible because one implementation of a garbage collection programfrees up memory used by objects to which no other object links. Thus,after the classes linking to the objects containing the constants havebeen garbage collected, the objects containing the constants can also begarbage collected.

[0021] Java is often run dynamically. Java classes are often loadeddynamically as they are needed. This loading may take place even over anetwork. Thus, the Java virtual machine can access classes from avariety of different locations, including local source, for example, ona hard drive, and from remote sources, for example, from a remote servervia the Internet. Instructions are stored as bytecode, and the bytecodeis executed by a Java Virtual Machine. The bytecode is lower level codethan a high level language that is run by an interpreter, and is higherlevel than a program compiled into machine code. The design of the Javaprogramming language helps to allow Java to run on a number of differentlocal systems, provided that the local systems have a Java VirtualMachine or equivalent. Thus, Java applications can be distributedwidely, for example via the Internet.

[0022]FIG. 1 is block diagram of a virtual machine, memory, and system,according to an embodiment of the invention. Source code 110 is providedto compiler 112. Compiler 112 outputs a set of class files 115 which maybe stored on server 114. System 124 receives class files 115 via network116. System 124 includes virtual machine 118, system interface 122, andRAM 120. RAM 120 includes classes 142, objects 171, 172, and 173, andhash table 180. Virtual machine 118 includes class loader 138, classparser 140, and execution engine 152.

[0023] Network 116 may comprise the Internet, or other network such as aLAN or an enterprise network. Virtual machine 118 may be implemented insoftware, hardware or both hardware and software. Virtual machine 118may comprise a Java Virtual machine or may comprise another systemcapable of executing the methods in the classes.

[0024] The class files 115 on server 114 include a constant pool foreach class. For example, class 126 includes constant pool 132, class 128includes constant pool 134, and class 130 includes constant pool 136.The constants stored within these constant pools within class files maybe redundant between the respective class files. The class files 115also include methods, which have bytecode instructions. Class loader 133loads respective class files 115 via network 116. Additionally, localstorage 160 may include class files, such class file 162, includingconstant pool 164. Local storage 160 may comprise flash memory, a harddrive, a CD, other memory device, or a combination of these. Classloader loads class files as they are needed dynamically as virtualmachine 118 requires. For example, some class files may be loadedinitially, and then, as a reference to another class is encounteredduring execution of the earlier loaded class files, additional classfiles are loaded. Some class files may be preloaded into memory. Insteadof RAM, according to an embodiment of the invention, some classes may bestored in ROM.

[0025] Class parser 140 parses through class files and stores the datafor the class files into RAM 120. RAM 120 may include the heap, suchthat classes 142 and global constant pool are stored on the heap. Inparsing classes, class parser 140 identifies constants from therespective constant pools of classes. Class parser 140 creates objects171, 172, and 173, and stores constants from the constant pools ofclasses into such objects.

[0026] According to one embodiment of the invention, only stringconstants are stored in such objects. Further, in one implementation,for commonly used constants, such as class name strings, a check isfirst made to determine whether the constant already exists in an object(such as an object among 171, 172, and 173). For less commonly usedstring constants, a new object is created for each constant withoutfirst checking to determine whether the constant already exists in anobject. An advantage of such an approach is that the processing time indetermining whether the constant has already been saved is avoided forconstants that are less likely stored in the objects. Hash table 180 isused to determine whether a constant has a corresponding object. Forexample, entries 181, 182, and 183 point to objects 171, 172, and 173respectively. When commonly used string constants from a new class areloaded, hash table 180 may be queried to determine links to thecorresponding objects. For example, entry 181 may have been used to findobject 171 when class 146 was loaded.

[0027] Constants from constant pools of classes 142 are replaced withlinks to the corresponding objects containing the constants. Forexample, in table 145 of class 144, link 174 to object 171 has replaceda constant. The corresponding constant is included in object 171. Class146 included the same constant, and that constant is replaced by link175 to object 171. Thus, object 171 is shared by class 144 and 146, andmemory is saved as a result. Link 176 from table 147 of class 146 linksto object 172. Object 172 includes the constant replaced by link 176.Link 177 from table 149 of class 148 links to object 173. Object 173includes the constant replaced by link 177. Hash table 180 points toobjects, such as objects 181, 182, and 183, with weak links. The garbagecollector typically ignores weak links when garbage collecting so thatif only a weak link points to an object, it can be garbage collected.Thus, if all the classes linking to an object are garbage collected,then the object can also be garbage collected. An advantage of such anapproach is that it allows constants to be shared between classes, thussaving memory, and for the constants to be garbage collected when notneeded, also resulting in a savings of memory.

[0028] Execution engine 152 supports object oriented code, including thecreation of instances and invocation of methods. Execution engine 152interprets the bytecodes from class 142 as required to execute requestedmethods. Execution engine 152 automatically follows links to theconstants stored in objects to find constants as needed during runtime.Execution engine is coupled to system interface 122, which executesoperations requested by execution engine 152. One embodiment, systeminterface 122 comprises an operating system, which may interface to afile system. And another embodiment to the invention, system interface122 is simply a file system, so that execution engine 152 is coupleddirectly to a file system, and the system 124 does not have an operatingsystem. Such a configuration is advantageous where there is a premium onmemory. And in this manner, in combination with the use of objects 171,172, 173, links, and lack of an operating system, the need for memory isreduced.

[0029] The functionality of the class parser 140 may be combined intoclass loader 138. In one embodiment of the invention, class loader 138represents the only class loader in virtual machine 118, and is modifiedto include the functionality of class parser 140. In such an embodiment,class loader 138 represents the system class loader. In an anotherembodiment of the invention, class loader 138, combined with thefunctionality of class parser 140 is a separate instance, and isindependent of the system class loader.

[0030] According to one embodiment of the invention, a class is parsedimmediately after it is loaded. According to another embodiment of theinvention, a class is parsed after a series of classes have been loadedby class loader 138. According to another embodiment of the invention,at least some classes are preparsed. In preparsing constants areidentified that can be stored separately from the classes. Theseconstants may be stored in ROM. The corresponding preparsed classes mayalso be stored in ROM. Such an approach has the advantage of furtherconserving the use of RAM.

[0031] In one embodiment of the invention, the structure described heremay be applicable to data structures other than Java class files. Forexample, in one embodiment of the invention, data structures other thanclass files 115 are stored on a server such as server 114. These datastructures include instructions and constants. These data structures mayalso be stored on local storage 160. When these data structures areloaded onto system 124, the instructions portions of the data structuresare stored separately in RAM 120 from at least some of the constants,which are stored, for example in objects 171, 172, and 173. Suchconstants can be shared between the various data structures.

[0032] System 124 in one embodiment of the invention is a smallappliance that has a minimum of RAM 120. System 124 may comprise a settop box that is coupled to a television, a game console, a web phone, orother small device. Another embodiment to the invention, system 124comprises a computer system, such as a desktop computer. Otherembodiments of system 124 are also possible.

[0033]FIG. 2 is a flow chart of a method of loading classes into memory,according to an embodiment of the invention. First start virtual machine(block 210). Load a class (block 214). Such a class may be obtained froma class file for example, over the Internet from a remote server or froma local storage. Next, isolate the constant pool of the class (block216).

[0034] According to one embodiment of the invention, only the stringentries of the constant pool are replaced by links to respective objectsoutside of the class. For example, as shown here, first determinewhether the member of the constant pool is a string entry (block 218).If the member is not a string, then proceed to block 230 to determinewhether there are additional constants from the constant pool. If themember is a string entry, then determine whether it is a commonly usedstring (block 220). If the member is not a commonly used string, thencreate an object for storing the string (block 226), without determiningwhether the string has already been stored in a separate object. If theentry is a commonly used string, then check a hash table to determine ifthe string has already been stored in an object (block 226). A commonlyused string may be, for example, a name of a class such as the name of aJava class. A string that is not a commonly used string, may be somearbitrary string, such as “hello world.” If the string has already beenstored as an object, then proceed to block 226 to create a link from thetable in the class to the object. If the string has not been alreadystored as an object, then create a hash table entry for the string(block 224). Create an object for storing the string (block 226), andthe hash table entry points to the object. Then, proceed to create alink from the table in the class to the object (block 228).

[0035] Such actions are performed for additional constants, providedthat additional constants are present in the class pool (see block 230).Next, additional classes may be loaded, and the actions described may beperformed on such additional classes. Alternatively, requested methodsmay be executed immediately (block 232). Thus, classes may be loadeddynamically and redundancies between constants from the various classescan be spared as such classes are loaded dynamically. An advantage tosuch an approach is that newly loaded classes can be efficiently storedin RAM by sharing certain constants between such newly loaded classesand constants already stored in memory (RAM of ROM).

[0036] Next, garbage collect string objects without links (block 234).Since such constants are stored in objects, one example of a garbagecollector automatically garbage collects such objects when the garbagecollector cannot find any links to those objects. Thus, when classeslinking to respective objects are garbage collected, the respectiveobjects including constants are garbage collected also.

[0037]FIG. 3 is a flow chart of a method of storing classes in ROM,according to an embodiment of the invention. According to FIG. 3,certain classes and constants are stored in ROM in order to conservespace in RAM. Various constants from the respective classes are storedseparately from the classes and shared among the respective classes inorder to save additional space in the ROM and also to save space in RAMas classes later on are loaded into RAM later.

[0038] First isolate the constant pool (block 310). If an entry in theconstant pool is not a string entry (block 312), then proceed to block321 to determine if additional constants are present in the constantpool. Otherwise, determine whether the member is a commonly used string(block 314). If the member is a commonly used string, then proceed toblock 316 to determine whether an object has already been created forthe string. If an object has already been created for the string, thenproceed to block 320 to create a link to the object including the stringfrom the class. If an object has not already been created for thestring, then create a hash table entry for the string (block 317). Thishash table entry can then later on be used to determine whether anobject including the string is present in memory so that the object neednot be created and instead a link to the existing object may be created.If the member is not a commonly used string (block 314), then proceed toblock 318 to create an object for storing the string. Thus, strings thatare not commonly used, according to one embodiment of the invention, arenot shared between different classes. Note that the determination madein block 316 regarding whether an object has already been created forthe string may be made by the hash table in which entries are created inblock 317. The various members of the constant pool are parsed accordingto the steps described until all constants have been parsed (block 321).

[0039] The objects containing the constants and the class are stored inROM (block 322). In this way, RAM is spared for these objects andconstants that are preprocessed. The objects in ROM containing constantsmay be reused by other classes that are loaded dynamically later onduring run time (as shown in block 328). Other commonly used strings, orother constants, may be stored as objects in ROM (block 324). Thus, adetermination may be made, without actually parsing respective classes,of constants that would be needed by various classes, and theseconstants may be stored in ROM. Eventually a virtual machine is started(block 326). As noted, links from tables loaded in classes during runtime may be created to the objects stored in ROM (block 328). In thisway, redundancies between commonly used constants from the respectiveclasses are avoided.

[0040] When constants are stored in objects separately from class files,certain optimizations may be implemented. For example, commonlyrepeating portions of constants may be replaced by shorter codes. Forexample, java/lang/may be replaced with a code, such as $1.

[0041] The methods and systems described above also apply inapplications other than Java classes. For example, these methods andsystems may be applicable to computer systems using other objectoriented programming schemes. Alternately, these methods and systems maymore generally be applied to the loading of data structures from memory,where the data structures include instructions and constants.

[0042] Various embodiments of the invention have been illustrated in thefigures and have been described in the corresponding text of thisapplication. This foregoing description is not intended to limit theinvention to the precise forms disclosed. Rather, the invention is to beconstrued to the full extent allowed by the following claims and theirequivalents.

What is claimed is:
 1. A method of operating a computer, the methodcomprising: for data structures in a set of data structures, receivingfrom a first memory a data structure from the set of data structures,the data structure including one or more sets of instructions and a setof one or more constants; storing the data structure in a second memory;if constants from the data structure have not been stored in other datastructures in the second memory, other than the first data structure,then storing constants from the data structure in data structures in thesecond memory, other than the first data structure in the second memory;and replacing the constants from the first data structure in the secondmemory with links to the respective other data structures in the secondmemory.
 2. The method of claim 1, wherein the data structure from thefirst memory comprises a Java class.
 3. The method of claim 1, whereinthe sets of instructions comprise Java methods.
 4. The method of claim1, wherein the constants from the data structure from the first memorycomprise a constant pool.
 5. The method of claim 1, wherein receivingthe data structure from a first memory comprises receiving the datastructure from a server over the Internet.
 6. The method of claim 1,including garbage collecting the respective other data structures in thesecond memory if all data structures linking to the respective otherdata structures have been or are to be garbage collected.
 7. The methodof claim 1, wherein constants from the data structure comprise stringsand constants other than strings, and wherein storing constants from thedata structure comprises storing only strings from the data structure inthe other data structures in the second memory.
 8. The method of claim1, wherein the second memory comprises ROM.
 9. A method of operating acomputer, the method comprising: for classes in a set of classes,receiving a class from a class file, the class including one or moremethods and a set of one or more constants; storing the class in amemory; if constants from the class have not been stored in objects inthe memory, other than in the class, then storing constants from theclass in objects in the memory, other than the class; and replacing theconstants from the class in the memory with links to the respectiveobjects in the memory.
 10. The method of claim 9, wherein the classescomprise Java classes.
 11. The method of claim 9, wherein receiving theclass from a class file comprises receiving the class from a server overthe Internet.
 12. The method of claim 9, wherein the constants comprisestrings.
 13. The method of claim 9, wherein the constants comprisestrings and constants other than strings, and storing constants from theclass in objects in the memory comprises storing only the strings inobjects in the memory.
 14. The method of claim 9, including creatingentries in a hash table corresponding to respective objects in whichconstants are stored and using the hash table to determine whetherconstants from the class have been stored in the objects.
 15. The methodof claim 9, including storing the objects in ROM.
 16. The method ofclaim 9, including storing the objects in RAM.
 17. The method of claim9, including garbage collecting objects in the memory if all classeslinking to the respective objects have been or are to be garbagecollected.
 18. The method of claim 9, including storing the objectsincluding the constant in the ROM, and after storing the objects in theROM, as classes are loaded during runtime, if constants from the classesloaded during runtime are stored in objects in the ROM, replacingconstants from the classes loaded during runtime with links to theobjects stored in the ROM.
 19. A method of operating a computer, themethod comprising: for classes in a set of classes, receiving a classfrom a class file, the class including one or more methods and a set ofone or more constants; storing the class in a memory; if constants fromthe class have been stored in objects in the memory, other than in theclass, then replacing the constants from the class that have been storedin objects with links to the respective objects, and if constants fromthe class have not been stored in objects in the memory, other than inthe class, then storing constants from the class in objects in thememory, other than the class, and replacing the constants from the classin the memory with links to the respective objects in the memory. 20.The method of claim 19, wherein the classes comprise Java classes. 21.The method of claim 19, including storing the objects in ROM.
 22. Themethod of claim 19, including garbage collecting objects in the memoryif all classes linking to the respective objects have been or are to begarbage collected.
 23. The method of claim 19, including storing theobjects including the constants in the ROM, and, after storing constantsin the ROM, as classes are loaded during runtime, if constants from theclasses loaded during runtime are stored in objects in the ROM,replacing constants from the classes loaded during runtime with links tothe objects stored in the ROM.
 24. A computer system comprising: aprocessor; a memory including, a set of objects, each object including aconstant, a set of classes, the classes corresponding to classes from aclass file in which classes have sets of methods and sets of constants,a first class in the set of classes having a link to an object in theset of objects, the object including a constant from a class in theclass file that corresponds to the first class, the first class in thememory not including the constant, and second class in the set ofclasses having a link to the object in the set of objects, a class inthe class file that corresponds to the second class having the constant,the second class in the memory not including the constant.
 25. Thecomputer system of claim 24, wherein the class in the class file thatcorresponds to the first class in the set of classes includes a constantpool including strings and constants other than strings and wherein thefirst class in the memory includes the constants other than strings andlinks to objects in the set of objects that include stringscorresponding to the strings in the constant pool in the class file. 26.The computer system of claim 24, including computer readable code thatloads classes from class files; stores the classes in the memory; andreplaces constants in the classes with links to respective objects inthe set of objects in the memory including the respective constants. 27.The computer system of claim 26, including computer readable code that,if a constant in a class loaded from a class file is not in an object inthe set of objects in the memory, stores an object in the memoryincluding the constant and replaces the constant in the class with alink to the object in the memory.
 28. The computer system of claim 27,including a hash table that has entries corresponding to objects in theset of objects, and, wherein the code that stores an object in memoryuses the hash table to determine whether to replace the constant in theclass with a link to an object in the set of objects.
 29. The computersystem of claim 26, the computer readable code replacing only stringconstants with links to respective objects in the memory.
 30. Thecomputer system of claim 24, including computer readable code that,loads classes from the class files, stores the classes from memory, andif a constant from a class is stored in an object in the set of objectsin the memory, the constant is a commonly used constant, then replacesthe constant in the class with a link to the object, and if the constantis not a commonly used constant, without determining whether theconstant is stored in an object in the set of objects in memory, storesa second object including the constant in the memory and replaces theconstant in the class with a link to the second object.
 31. The computersystem of claim 30, wherein the commonly used constant comprises a nameof a class.
 32. The computer system of claim 24, including garbagecollection code.